import http from '@/utils/http';
import type { 
  User, 
  PaginatedResponse, 
  UserQueryParams, 
  CreateUserRequest, 
  UpdateUserRequest, 
  ResetPasswordRequest, 
  BatchUpdateStatusRequest,
  UserStatistics,
  ApiPaginatedResponse
} from '@/types';

/**
 * 获取用户列表（支持搜索和分页）
 */
export const getUserList = async (params: UserQueryParams): Promise<ApiPaginatedResponse<User>> => {
  const response = await http.get('/admin/users', { params });
  return response.data;
};

/**
 * 获取用户详情
 */
export const getUserDetail = async (id: number): Promise<User> => {
  const response = await http.get(`/admin/users/${id}`);
  return response.data.data;
};

/**
 * 创建用户
 */
export const createUser = async (data: CreateUserRequest): Promise<User> => {
  const response = await http.post('/admin/users', data);
  return response.data.data;
};

/**
 * 更新用户信息
 */
export const updateUser = async (id: number, data: UpdateUserRequest): Promise<User> => {
  const response = await http.put(`/admin/users/${id}`, data);
  return response.data.data;
};

/**
 * 重置用户密码
 */
export const resetUserPassword = async (id: number, data: ResetPasswordRequest): Promise<void> => {
  await http.post(`/admin/users/${id}/reset-password`, data);
};

/**
 * 批量更新用户状态
 */
export const batchUpdateUserStatus = async (data: BatchUpdateStatusRequest): Promise<{
  updatedCount: number;
  status: string;
}> => {
  const response = await http.post('/admin/users/batch-update-status', data);
  return response.data.data;
};

/**
 * 删除用户（软删除）
 */
export const deleteUser = async (id: number): Promise<void> => {
  await http.delete(`/admin/users/${id}`);
};

/**
 * 批量删除用户
 */
export const batchDeleteUsers = async (ids: number[]): Promise<{
  deletedCount: number;
}> => {
  const response = await http.post('/admin/users/batch-delete', { ids });
  return response.data.data;
};

/**
 * 获取用户统计信息
 */
export const getUserStatistics = async (): Promise<UserStatistics> => {
  const response = await http.get('/admin/users/statistics/overview');
  return response.data.data;
}; 